package com.shuma.upms.user.mapper;

import com.shuma.upms.user.domain.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserMapper {

    // 新增用户
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    @Insert("INSERT INTO users (username, password, email, phone, gmt_create) VALUES (#{username}, #{password}, #{email}, #{phone}, now())")
    int insert(User user);

    // 根据用户名查询用户
    @Select("SELECT * FROM users WHERE username = #{username}")
    User selectByUsername(@Param("username") String username);

    // 根据用户ID查询用户
    @Select("SELECT * FROM users WHERE user_id = #{userId}")
    User selectById(@Param("userId") Long userId);

    // 查询所有用户
    @Select("SELECT * FROM users")
    List<User> selectAll();

    // 更新用户基本信息
    @Update("<script>" +
            "UPDATE users " +
            "<set>" +
            "<if test='username != null'>username = #{username},</if>" +
            "<if test='email != null'>email = #{email},</if>" +
            "<if test='phone != null'>phone = #{phone},</if>" +
            "</set>" +
            "WHERE user_id = #{userId}" +
            "</script>")
    int updateUser(User user);

    // 更新用户密码
    @Update("UPDATE users SET password = #{newPassword} WHERE user_id = #{userId}")
    int updatePassword(@Param("userId") Long userId, @Param("newPassword") String newPassword);

     //根据用户ID列表查询用户信息 (用于管理员分页)
    @Select("<script>" +
            "SELECT user_id, username, password, email, phone, gmt_create " +
            "FROM users " +
            "WHERE user_id IN " +
            "<foreach item='userId' collection='userIds' open='(' separator=',' close=')'>" +
            "#{userId}" +
            "</foreach>" +
            "</script>")
    List<User> selectUsersByIds(@Param("userIds") List<Long> userIds);


    //查询所有用户的ID列表
    @Select("SELECT user_id FROM users")
    List<Long> selectAllUserIds();


}